// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//


#include <pollux/common/caching/string_id_map.h>

#include <gtest/gtest.h>
#include <pollux/testing/gtest_utils.h>

using namespace kumo::pollux;

TEST(StringIdMapTest, basic) {
  constexpr const char* kFile1 = "file_1";
  StringIdMap map;
  uint64_t id = 0;
  {
    StringIdLease lease1;
    EXPECT_FALSE(lease1.hasValue());
    StringIdLease lease2(map, kFile1);
    EXPECT_TRUE(lease2.hasValue());
    id = lease2.id();
    lease1 = lease2;
    EXPECT_EQ(id, lease1.id());
    EXPECT_EQ(strlen(kFile1), map.pinnedSize());
  }
  StringIdLease lease3(map, kFile1);
  EXPECT_NE(lease3.id(), id);
  lease3.clear();
  EXPECT_EQ(0, map.pinnedSize());
}

TEST(StringIdMapTest, rehash) {
  constexpr int32_t kCount = 10000;
  StringIdMap map;
  std::vector<StringIdLease> ids;
  for (auto i = 0; i < kCount; ++i) {
    auto name = fmt::format("filename_{}", i);
    ids.push_back(StringIdLease(map, name));
  }
  for (auto i = 0; i < kCount; ++i) {
    auto name = fmt::format("filename_{}", i);
    EXPECT_EQ(ids[i].id(), StringIdLease(map, name).id());
  }
}

TEST(StringIdMapTest, recover) {
  constexpr const char* kRecoverFile1 = "file_1";
  constexpr const char* kRecoverFile2 = "file_2";
  constexpr const char* kRecoverFile3 = "file_3";
  StringIdMap map;
  const uint64_t recoverId1{10};
  const uint64_t recoverId2{20};
  {
    StringIdLease lease(map, recoverId1, kRecoverFile1);
    ASSERT_TRUE(lease.hasValue());
    ASSERT_EQ(map.pinnedSize(), ::strlen(kRecoverFile1));
    ASSERT_EQ(map.testingLastId(), recoverId1);
    POLLUX_ASSERT_THROW(
        std::make_unique<StringIdLease>(map, recoverId1, kRecoverFile2),
        "(1 vs. 0) Reused recover id 10 assigned to file_2");
    POLLUX_ASSERT_THROW(
        std::make_unique<StringIdLease>(map, recoverId2, kRecoverFile1),
        "(20 vs. 10) Multiple recover ids assigned to file_1");
  }
  ASSERT_EQ(map.pinnedSize(), 0);

  StringIdLease lease1(map, kRecoverFile1);
  ASSERT_EQ(map.pinnedSize(), ::strlen(kRecoverFile1));
  ASSERT_EQ(map.testingLastId(), recoverId1 + 1);

  {
    StringIdLease lease(map, recoverId2, kRecoverFile2);
    ASSERT_TRUE(lease.hasValue());
    ASSERT_EQ(lease.id(), recoverId2);
    ASSERT_EQ(
        map.pinnedSize(), ::strlen(kRecoverFile1) + ::strlen(kRecoverFile2));
    ASSERT_EQ(map.testingLastId(), recoverId2);
    POLLUX_ASSERT_THROW(
        std::make_unique<StringIdLease>(map, recoverId2, kRecoverFile3),
        "(1 vs. 0) Reused recover id 20 assigned to file_3");
    POLLUX_ASSERT_THROW(
        std::make_unique<StringIdLease>(map, recoverId2, kRecoverFile1),
        "(20 vs. 11) Multiple recover ids assigned to file_1");
    StringIdLease dupLease(map, recoverId2, kRecoverFile2);
    ASSERT_TRUE(lease.hasValue());
    ASSERT_EQ(lease.id(), recoverId2);
    ASSERT_EQ(
        map.pinnedSize(), ::strlen(kRecoverFile1) + ::strlen(kRecoverFile2));
  }

  ASSERT_EQ(map.testingLastId(), recoverId2);
  ASSERT_EQ(map.pinnedSize(), ::strlen(kRecoverFile1));
}
